package com.orhon.smartcampus.modules.systemctl.fetcher;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.systemctl.dao.MenusDao;
import com.orhon.smartcampus.modules.systemctl.entity.Apis;
import com.orhon.smartcampus.modules.systemctl.entity.Menus;
import com.orhon.smartcampus.modules.systemctl.entity.Modules;
import com.orhon.smartcampus.modules.systemctl.entity.Widgets;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLApis;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLMenus;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLModule;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLWidgets;
import com.orhon.smartcampus.modules.systemctl.mm.ApisMapper;
import com.orhon.smartcampus.modules.systemctl.mm.MenuMapper;
import com.orhon.smartcampus.modules.systemctl.mm.ModuleMapper;
import com.orhon.smartcampus.modules.systemctl.mm.WidgetsMapper;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.Util;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class MenusFetcher implements DataFetcher {


    @Autowired
    MenusDao menusDao;

    @Override
    public Object get(DataFetchingEnvironment env) throws Exception {
        
        List<GQLMenus> Menus = new ArrayList<>();


		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
		String hql = "from Menus where deleted_at = null ";

		if(env.containsArgument("condition")) {
			HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
			if(map.get("module_id")!=null) {
				hql += "and module_id " + Util.decide(map.get("module_id"));
			}
			if(map.get("category")!=null) {
				hql += "and category "+ Util.decide(map.get("category"));
			}
			if(map.get("type")!=null) {
				hql += "and type "+ Util.decide(map.get("type"));
			}
			if(map.get("mark")!=null) {
				hql += "and name "+ Util.decide(map.get("mark"));
			}
			if(map.get("client")!=null) {
				hql += "and clients "+ Util.decide(map.get("client"));
			}

			if(map.get("parent")!=null) {
				hql += "and parent_id " +  Util.decide(map.get("parent"));
			}
			if(map.get("status")!=null) {
				hql += "and  status " + Util.decide(map.get("status"));
			}
			

		}
		hql += " ORDER BY ordered ASC ";
		List<Menus> list = menusDao.find(hql,null,page.getPage(),page.getLimit());


		for (Menus item : list) {
			GQLMenus gqlMenus = null;
			if (env.getSelectionSet().contains("data/parent")){
				Menus menus = item.getMenus();
				gqlMenus = MenuMapper.MAPPER.toMenus(menus);
			}
			GQLModule gqlModule = null;
			if (env.getSelectionSet().contains("data/module")){
				Modules modules = item.getModules();
				gqlModule = ModuleMapper.MAPPER.mapModule(modules);
			}

			List<GQLWidgets> GQLWidgets= null;
			if (env.getSelectionSet().contains("data/widgets")){
				List<Widgets> widgets = item.getWidgets();
				GQLWidgets = WidgetsMapper.MAPPER.toWidgetsList(widgets);
			}

			List<GQLMenus> GQLMenus= null;
			if (env.getSelectionSet().contains("data/childreen")){
				List<Menus> lists = item.getChildreen();
				GQLMenus = MenuMapper.MAPPER.toMenusList(lists);
			}

			List<GQLApis> GQLApis= null;
			if (env.getSelectionSet().contains("data/operationApiRelations")){
				List<Apis> lists = item.getApis();
				GQLApis = ApisMapper.MAPPER.toApisList(lists);
			}
			GQLMenus menus = MenuMapper.MAPPER.toMenus(item);
			menus.setParent(gqlMenus);
			menus.setModule(gqlModule);
			menus.setWidgets(GQLWidgets);
			menus.setChildreen(GQLMenus);
			menus.setApis(GQLApis);
			Menus.add(menus);
		}

		Long total = 0L;
		if (env.getSelectionSet().contains("total")){
			total = menusDao.getTotalCount(hql);
		}
		return Result.ok().put("data", Menus).put("total", total);
    }



}
